/*
 * NewGoodsFragment.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.newgoods;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseEventFragment;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.ui.widget.recyclerview.LoadMoreRecyclerContainer;
import com.yidiandao.business.ui.widget.recyclerview.MultiTypeLoadMoreAdapter;
import com.yidiandao.business.ui.widget.recyclerview.provider.GoodsGridProvider;
import com.yidiandao.business.ui.widget.recyclerview.provider.GoodsListProvider;
import com.yidiandao.business.utils.AnimatorUtils;
import com.yidiandao.business.utils.SysUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 首页-新品-View
 */
public class NewGoodsFragment extends SuperBaseEventFragment implements NewGoodsContract.View {

    private static final String TAG = NewGoodsFragment.class.getSimpleName();

    private NewGoodsContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private PtrClassicFrameLayout mPtrFrame;

    private ImageView mIvToTop;

    private RelativeLayout mRlytGridStyle;

    private ImageView mIvGridStyle;

    private RecyclerView mRecyclerView;

    private LoadMoreRecyclerContainer mLoadMoreContainer;

    private MultiTypeLoadMoreAdapter mGridAdapter;

    private MultiTypeLoadMoreAdapter mListAdapter;

    private boolean isVisibility;

    private boolean isOpening, isHiding;

    private boolean isGridStyle;

    public static NewGoodsFragment getInstance() {
        return new NewGoodsFragment();
    }

    @Override
    public void setPresenter(NewGoodsContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mPtrFrame = (PtrClassicFrameLayout) mRootView.findViewById(R.id.ptr_frame);
        mLoadMoreContainer =
                (LoadMoreRecyclerContainer) mRootView.findViewById(R.id.load_more_container);
        mIvToTop = (ImageView) mRootView.findViewById(R.id.iv_to_top);
        mRlytGridStyle = (RelativeLayout) mRootView.findViewById(R.id.rlyt_grid_style);
        mIvGridStyle = (ImageView) mRootView.findViewById(R.id.iv_grid_style);

        initRecyclerView();
        initListeners();
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    private void initRecyclerView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mGridAdapter = new MultiTypeLoadMoreAdapter();
        mListAdapter = new MultiTypeLoadMoreAdapter();

        mGridAdapter.register(DoubleGoods.class, new GoodsGridProvider(getActivity()));
        mListAdapter.register(Goods.class, new GoodsListProvider(getActivity()));

        // 初始化为GridStyle
        changeAdapterStyle(false);
    }

    private void initListeners() {

        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });

        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(
                new PtrDefaultHandler() {
                    @Override
                    public void onRefreshBegin(PtrFrameLayout frame) {
                        // 下拉刷新
                        mPresenter.loadNewGoodsList(true);
                    }

                    @Override
                    public boolean checkCanDoRefresh(
                            PtrFrameLayout frame, View content, View header) {
                        return PtrDefaultHandler.checkContentCanBePulledDown(
                                frame, mRecyclerView, header);
                    }
                });

        mLoadMoreContainer.setLoadMoreHandler(
                new LoadMoreHandler() {
                    @Override
                    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                        // 加载更多
                        mPresenter.loadNewGoodsList(false);
                    }
                });

        mLoadMoreContainer.setOnScrollListener(
                new RecyclerView.OnScrollListener() {

                    private int totalDy;

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        totalDy -= dy;
                        if (dy > 0) {
                            // 动画隐藏
                            hideAnimator(mIvToTop);
                        } else {
                            if (Math.abs(totalDy) > SysUtils.getScreenHeight(
                                    UIUtils.getContext())) {
                                // 超过屏幕高度，开始显示
                                // 动画显示
                                showAnimator(mIvToTop);
                            } else {
                                // 隐藏
                                // 动画隐藏
                                hideAnimator(mIvToTop);
                            }
                        }
                    }
                });

        mIvToTop.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 返回顶部
                        mRecyclerView.smoothScrollToPosition(0);
                    }
                });

        mRlytGridStyle.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isGridStyle = !isGridStyle;
                        changeAdapterStyle(isGridStyle);
                        mPresenter.changeStyle(isGridStyle);
                    }
                });
    }

    private void changeAdapterStyle(boolean isGridStyle) {
        // 当前是否是网格状布局
        mLoadMoreContainer.setRecyclerViewAdapter(isGridStyle ? mListAdapter : mGridAdapter);
        mLoadMoreContainer.useDefaultFooter();
        mIvGridStyle.setSelected(isGridStyle);
    }

    /**
     * 显示动画
     */
    private void showAnimator(View view) {
        AnimatorUtils.showToTopAnimator(
                view,
                isVisibility,
                isOpening,
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        isOpening = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        isVisibility = true;
                        isOpening = false;
                    }
                });
    }

    /**
     * 隐藏动画
     */
    private void hideAnimator(final View view) {
        AnimatorUtils.hideToTopAnimator(
                view,
                isVisibility,
                isHiding,
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        isHiding = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        view.setVisibility(View.GONE);
                        isVisibility = false;
                        isHiding = false;
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_new_goods;
    }

    @Override
    public void setGoodsList(List<Goods> goodsList) {
        mListAdapter.setItems(goodsList);
        mListAdapter.notifyDataSetChanged();
    }

    @Override
    public void setGoodsGrid(List<DoubleGoods> goodsGridList) {
        mGridAdapter.setItems(goodsGridList);
        mGridAdapter.notifyDataSetChanged();
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public void notifyDataChanged() {
        mPresenter.start();
    }

    @Override
    public void setRefreshComplete() {
        mPtrFrame.refreshComplete();
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }
}
